Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return json error response body as object #238

Merged
merged 1 commit into from
Feb 27, 2023

Conversation

dpopp07
Copy link
Member

@dpopp07 dpopp07 commented Feb 21, 2023

Error response bodies are currently returned as strings, even when the content is JSON. This is a bug - the SDK should return the result as an object. This commit adds logic to return the result as an object, converting it if necessary.

It also updates the error object to use the "result" field for the response body, in order to be consistent with the success path. The "body" field is still set for backward-compatibility but is marked as deprecated.

Additionally, for both success and error responses, if the content is JSON but the result cannot be parsed as an object, an exception will be raised, alerting the user that the service returned a malformed body.

cc @ricellis

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks great, but I think we need to be tolerant of content-type values that might contain additional context stuff at the end.
We do this in a few other places (generator, cores, etc.) and we might want to also introduce this approach in the node core. We probably have several other situations (either in the node core or other repos) where we should be doing regex matches instead of strict "===", so it's probably not critical that we do this now in this PR. Just mentioning this since I noticed it while reviewing this PR.

*/
function contentTypeIsJSON(headers: IncomingHttpHeaders): boolean {
const contentType = headers['content-type'] || '';
return contentType.toLowerCase() === 'application/json';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think whenever we check Content-Type for specific values, rather than doing a strict "===", we should tolerate mimetypes like application/json; charset=utf8. In most situations (generator, cores, etc.) where we need to check Content-Type, we use a regex type comparison to account for the optional stuff at the end.

I did a quick check of the node core code, and I don't think we have anything like this place yet. Perhaps we could just introduce a utility function isJsonMimetype().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, great call. I thought about that after I made the changes but I forgot to go back and implement it. I'm glad you pointed that out!

it('should convert string response bodies to objects if content is JSON', () => {
const newAxiosError = makeCopy(basicAxiosError);
newAxiosError.response.data = '{ "errorMessage": "some error" }';
expect(newAxiosError.response.headers['content-type']).toBe('application/json');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try a content-type that has extra stuff (e.e. application/json; charset=utf8.

@dpopp07
Copy link
Member Author

dpopp07 commented Feb 23, 2023

Okay @padamstx, your comments have been addressed!

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

lib/helper.ts Outdated Show resolved Hide resolved
lib/request-wrapper.ts Outdated Show resolved Hide resolved
Error response bodies are currently returned as strings, even when the content is JSON.
This is a bug - the SDK should return the result as an object. This commit adds logic
to return the result as an object, converting it if necessary.

It also updates the error object to use the "result" field for the response body, in
order to be consistent with the success path. The "body" field is still set for
backward-compatibility but is marked as deprecated.

Additionally, for both success and error responses, if the content is JSON but the
result cannot be parsed as an object, an exception will be raised, alerting the user
that the service returned a malformatted body.

Signed-off-by: Dustin Popp <dpopp07@gmail.com>
@padamstx
Copy link
Member

Latest changes look good (I already approved).

@dpopp07 dpopp07 merged commit 6f201b7 into main Feb 27, 2023
@dpopp07 dpopp07 deleted the dp/json-error-responses branch February 27, 2023 21:37
ibm-devx-sdk pushed a commit that referenced this pull request Feb 27, 2023
## [4.0.4](v4.0.3...v4.0.4) (2023-02-27)

### Bug Fixes

* return json error response body as object ([#238](#238)) ([6f201b7](6f201b7))
@ibm-devx-sdk
Copy link

🎉 This PR is included in version 4.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants